<?php

/**
 * This is the model class for table "{{article}}".
 *
 * The followings are the available columns in table '{{article}}':
 * @property string $id
 * @property integer $city_id
 * @property string $token
 * @property integer $is_link
 * @property integer $user_id
 * @property string $title
 * @property integer $is_recommend
 * @property integer $is_good
 * @property integer $is_show
 * @property integer $listorder
 * @property integer $update_time
 * @property integer $create_time
 * @property integer $click_num
 * @property integer $share_num
 * @property integer $footmark
 * @property string $type
 * @property string $description
 * @property double $price
 * @property string $phone
 * @property string $address
 * @property string $thumb
 * @property integer $area
 * @property integer $level
 * @property string $people
 * @property string $theme
 * @property string $pick
 * @property integer $frond_hot
 * @property integer $frond_recommend
 */
class Article extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{article}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('city_id, user_id, title, update_time, create_time', 'required'),
			array('city_id, is_link, user_id, is_recommend, is_good, is_show, listorder, update_time, create_time, click_num, share_num, footmark, area, level, frond_hot, frond_recommend', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			array('token', 'length', 'max'=>20),
			array('title', 'length', 'max'=>100),
			array('type', 'length', 'max'=>9),
			array('description, address', 'length', 'max'=>255),
			array('phone, people, theme, pick', 'length', 'max'=>50),
			array('thumb', 'length', 'max'=>120),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, city_id, token, is_link, user_id, title, is_recommend, is_good, is_show, listorder, update_time, create_time, click_num, share_num, footmark, type, description, price, phone, address, thumb, area, level, people, theme, pick, frond_hot, frond_recommend', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'city_id' => 'City',
			'token' => 'Token',
			'is_link' => 'Is Link',
			'user_id' => 'User',
			'title' => 'Title',
			'is_recommend' => 'Is Recommend',
			'is_good' => 'Is Good',
			'is_show' => 'Is Show',
			'listorder' => 'Listorder',
			'update_time' => 'Update Time',
			'create_time' => 'Create Time',
			'click_num' => 'Click Num',
			'share_num' => 'Share Num',
			'footmark' => 'Footmark',
			'type' => 'Type',
			'description' => 'Description',
			'price' => 'Price',
			'phone' => 'Phone',
			'address' => 'Address',
			'thumb' => 'Thumb',
			'area' => 'Area',
			'level' => 'Level',
			'people' => 'People',
			'theme' => 'Theme',
			'pick' => 'Pick',
			'frond_hot' => 'Frond Hot',
			'frond_recommend' => 'Frond Recommend',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('token',$this->token,true);
		$criteria->compare('is_link',$this->is_link);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('is_recommend',$this->is_recommend);
		$criteria->compare('is_good',$this->is_good);
		$criteria->compare('is_show',$this->is_show);
		$criteria->compare('listorder',$this->listorder);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('click_num',$this->click_num);
		$criteria->compare('share_num',$this->share_num);
		$criteria->compare('footmark',$this->footmark);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('thumb',$this->thumb,true);
		$criteria->compare('area',$this->area);
		$criteria->compare('level',$this->level);
		$criteria->compare('people',$this->people,true);
		$criteria->compare('theme',$this->theme,true);
		$criteria->compare('pick',$this->pick,true);
		$criteria->compare('frond_hot',$this->frond_hot);
		$criteria->compare('frond_recommend',$this->frond_recommend);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getList($city_id,$type){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->addCondition("type='$type'");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getSearchList($city_id,$type,$keyword){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->addCondition("type='$type'");
        $criteria->addSearchCondition('title',$keyword,true,'AND');
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getKeywordCountList($city_id,$keyword){
        $db = Yii::app()->db->createCommand();
        $db->select('count(*) as count');
        $db->from('{{article}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show',array(':city_id'=>$city_id,':is_show'=>1));
        $db->andWhere(array('like', 'title', '%'.$keyword.'%'));
        $info = $db->queryRow();
        return $info['count'];
    }
    public function getKeywordList($city_id,$keyword,$first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('title,token,description,type,price,phone,address,thumb,area,level,people,theme');
        $db->from('{{article}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show',array(':city_id'=>$city_id,':is_show'=>1));
        $db->andWhere(array('like', 'title', '%'.$keyword.'%'));
        $db->order('id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getFrontCountList($city_id,$type,$theme = '',$area = '',$people = '',$level = '',$pick = ''){
        $db = Yii::app()->db->createCommand();
        $db->select('count(*) as count');
        $db->from('{{article}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and type=:type',array(':city_id'=>$city_id,':type'=>$type,':is_show'=>1));
        if($theme){
            $db->andWhere('FIND_IN_SET(:theme,theme)',array(':theme'=>$theme));
        }
        if($area){
            $db->andWhere('area=:area',array(':area'=>$area));
        }
        if($people){
            $db->andWhere('FIND_IN_SET(:people,people)',array(':people'=>$people));
        }
        if($level){
            $db->andWhere('level=:level',array(':level'=>$level));
        }
        if($pick){
            $db->andWhere('FIND_IN_SET(:pick,pick)',array(':pick'=>$pick));
        }
        $info = $db->queryRow();
        return $info['count'];
    }
    public function getFrontList($city_id,$type,$first,$total,$theme = '',$area = '',$people = '',$level = '',$pick = ''){
        $db = Yii::app()->db->createCommand();
        $db->select('title,token,description,price,phone,address,thumb,area,level,people,theme');
        $db->from('{{article}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and type=:type',array(':city_id'=>$city_id,':type'=>$type,':is_show'=>1));
        if($theme){
            $db->andWhere('FIND_IN_SET(:theme,theme)',array(':theme'=>$theme));
        }
        if($area){
            $db->andWhere('area=:area',array(':area'=>$area));
        }
        if($people){
            $db->andWhere('FIND_IN_SET(:people,people)',array(':people'=>$people));
        }
        if($level){
            $db->andWhere('level=:level',array(':level'=>$level));
        }
        if($pick){
            $db->andWhere('FIND_IN_SET(:pick,pick)',array(':pick'=>$pick));
        }
        $db->order('is_recommend desc,listorder desc,id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getFrontIndex($city_id,$limit,$type){
        $db = Yii::app()->db->createCommand();
        $db->select('title,address,token,thumb,price');
        $db->from("{{article}}");
        $db->andWhere('city_id=:city_id and is_show=:is_show and frond_recommend=:frond_recommend and type=:type',array(':city_id'=>$city_id,':is_show'=>1,':frond_recommend'=>1,':type'=>$type));
        $db->order("listorder desc");
        $db->limit($limit);
        $info = $db->queryAll();
        return $info;
    }
}
